<?php
	$fileInName = "book.txt";
	$fileIn = fopen($fileInName, 'r');
	
	$arr = array('第', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十');
	
	$list = '序.txt';
	$list = iconv('UTF-8', 'GBK', $list);
	$newfilename = fopen($list, 'a');
	
	while (!feof($fileIn)) {
		$fileRow = fgets($fileIn);
		$fileRow = rtrim($fileRow, " ");						//删除末尾空格
		$fileRow = preg_replace("/^\r?\n/","",$fileRow);		//删除空
		$str2 = $fileRow;
		$str2 = trim($str2, " ");
		
		foreach($arr as $key=>$value) {
			$pos = mb_strpos($fileRow, $value, 0, 'UTF-8');		//定位
			if($pos === 0) {
				$str1 = $fileRow;
				$fileRow = trim($fileRow);
				$fileRow = iconv('UTF-8', 'GBK', $fileRow);		//编码转换
				$file = $fileRow.".txt";
				$newfilename = fopen($file, 'a');
				$fileRow = $fileRow."\r\n";
				fwrite($newfilename, $fileRow);
			}
		}
		//插当前入章节内容
		if($str1 != $str2) {
			$fileRow = iconv('UTF-8', 'GBK', $fileRow);
			fwrite($newfilename, $fileRow);
		} else {
			echo "1";
		}
	}
	
	fclose($newfilename);
	fclose($fileIn);